<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.9.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="redis集群
概念：
为什么要集群？
虽然之前搞了主从部署，一主可以多从，但如果同时访问量(1000w)过大，主服务肯定就会挂掉，数据服务就挂掉了或者发生自然灾变
所以一些大公司都会部署很多服务器，如(华东地区、华北地区、西北地区、东北地区等都设立机房)


集群概念：
集群是一组相互独立的、通过">
    

    <!--Author-->
    
        <meta name="author" content="ck">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="redis集群+与python交互">
    

    <!--Open Graph Description-->
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="CK&#39;s blogs">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    

    <meta name="twitter:card" content="summary">
    

    <!-- Title -->
    
    <title>redis集群+与python交互 - CK&#39;s blogs</title>

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Google Analytics -->
    


</head>


<body>

<div class="bg-gradient"></div>
<div class="bg-pattern"></div>

<!-- Menu -->
<!--Menu Links and Overlay-->
<div class="menu-bg">
    <div class="menu-container">
        <ul>
            
            <li class="menu-item">
                <a href="/blog/">
                    Home
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/archives">
                    Archives
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/2019/09/04/本人简历/">
                    About
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/tags">
                    Tags
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/categories">
                    Categories
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/contact.html">
                    Contact
                </a>
            </li>
            
        </ul>
    </div>
</div>

<!--Hamburger Icon-->
<nav>
    <a href="#menu"></a>
</nav>

<div class="container">

    <!-- Main Content -->
    <div class="row">
    <div class="col-sm-12">

        <!--Title and Logo-->
        <header>
    <div class="logo">
        <a href="/blog/"><i class="logo-icon fa fa-cube" aria-hidden="true"></i></a>
        
    </div>
</header>

        <section class="main">
            
<div class="post">

    <div class="post-header">
        <h1 class="title">
            <a href="/blog/2019/09/05/redis集群-与python交互/">
                redis集群+与python交互
            </a>
        </h1>
        <div class="post-info">
            
                <span class="date">2019-09-05</span>
            
            
            
        </div>
    </div>

    <div class="content">

        <!-- Gallery -->
        

        <!-- Post Content -->
        <h1 id="redis集群"><a href="#redis集群" class="headerlink" title="redis集群"></a><code>redis</code>集群</h1><ul>
<li>概念：<ul>
<li>为什么要集群？<ul>
<li>虽然之前搞了主从部署，一主可以多从，但如果同时访问量(1000w)过大，主服务肯定就会挂掉，数据服务就挂掉了或者发生自然灾变</li>
<li>所以一些大公司都会部署很多服务器，如(华东地区、华北地区、西北地区、东北地区等都设立机房)</li>
</ul>
</li>
<li>集群概念：<ul>
<li>集群是一组相互独立的、通过高速网络互连的计算机，它们构成了一个组，并以单一系统的模式加以管理，一个客户与集群相互作用时，集群就像是一个独立的服务器，集群配置适用于提高可用性和可缩放性。</li>
<li>简单来说，就是一组通过网络连接的计算机，共同对外提交服务，像一个独立的服务器，目的就是提高处理性能</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="分类"><a href="#分类" class="headerlink" title="分类"></a>分类</h2><h3 id="软件层面"><a href="#软件层面" class="headerlink" title="软件层面"></a>软件层面</h3><ul>
<li>只有一台电脑，在这台电脑上启动了多个<code>redis</code>服务,服务之间组成集群，但是很容易坏掉，一个服务坏掉整个集群不能使用</li>
</ul>
<h3 id="硬件层面"><a href="#硬件层面" class="headerlink" title="硬件层面"></a>硬件层面</h3><ul>
<li>存在多台实体的电脑，每台电脑都启动了一个<code>redis</code>或者多个<code>redis</code>服务</li>
</ul>
<h3 id="在root用户下"><a href="#在root用户下" class="headerlink" title="在root用户下"></a>在root用户下</h3><h3 id="机器1"><a href="#机器1" class="headerlink" title="机器1"></a>机器1</h3><ul>
<li><p>建立一个<code>conf</code>的文件夹</p>
<ul>
<li><code>mkdir conf</code></li>
<li><code>cd conf</code></li>
</ul>
</li>
<li><p>在<code>conf</code>目录下创建 <code>7000.conf</code>文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">port <span class="number">7000</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7000.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7000</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>
</li>
<li><p>在<code>conf</code>目录下创建<code>7001.conf</code> 文件</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6</span>port <span class="number">7001</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7001.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7001</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>

<ul>
<li>在<code>conf</code>目录下创建 <code>7002.conf</code>文件</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">port <span class="number">7002</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7002.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7002</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>

<h4 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h4><ul>
<li>三个文件的配置区别在于<code>port、pidfile、cluster-config-file</code>三项</li>
</ul>
<h4 id="使用配置文件启动服务"><a href="#使用配置文件启动服务" class="headerlink" title="使用配置文件启动服务"></a>使用配置文件启动服务</h4><ul>
<li><code>redis-server 7000.conf</code></li>
<li><code>redis-server 7001.conf</code></li>
<li><code>redis-server 7002.conf</code></li>
</ul>
<h4 id="查看进程"><a href="#查看进程" class="headerlink" title="查看进程"></a>查看进程</h4><ul>
<li><code>ps aux | grep redis</code></li>
</ul>
<h3 id="机器2"><a href="#机器2" class="headerlink" title="机器2"></a>机器2</h3><ul>
<li><p>建立一个<code>conf</code>的文件夹</p>
<ul>
<li><code>mkdir conf</code></li>
<li><code>cd conf</code></li>
</ul>
</li>
<li><p>在<code>conf</code>目录下创建 <code>7003.conf</code>文件</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">port <span class="number">7003</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7003.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7003</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>
</li>
<li><p>在<code>conf</code>目录下创建<code>7004.conf</code> 文件</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6</span>port <span class="number">7004</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7004.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7004</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>

<ul>
<li>在<code>conf</code>目录下创建 <code>7005.conf</code>文件</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">port <span class="number">7005</span></span><br><span class="line">bind 本地IP</span><br><span class="line">daemonize yes</span><br><span class="line">pidfile <span class="number">7005.</span>pid</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file <span class="number">7005</span>_node.conf</span><br><span class="line">cluster-node-timeout <span class="number">15000</span></span><br><span class="line">appendonly yes</span><br></pre></td></tr></table></figure>

<h4 id="总结：-1"><a href="#总结：-1" class="headerlink" title="总结："></a>总结：</h4><ul>
<li>三个文件的配置区别在于<code>port、pidfile、cluster-config-file</code>三项</li>
</ul>
<h4 id="使用配置文件启动服务-1"><a href="#使用配置文件启动服务-1" class="headerlink" title="使用配置文件启动服务"></a>使用配置文件启动服务</h4><ul>
<li><code>redis-server 7003.conf</code></li>
<li><code>redis-server 7004.conf</code></li>
<li><code>redis-server 7005.conf</code></li>
</ul>
<h4 id="查看进程-1"><a href="#查看进程-1" class="headerlink" title="查看进程"></a>查看进程</h4><ul>
<li><code>ps aux | grep redis</code></li>
</ul>
<p>注：现在两台机器开启了6个<code>redis</code>服务</p>
<h3 id="创建集群"><a href="#创建集群" class="headerlink" title="创建集群"></a>创建集群</h3><ul>
<li><p><code>redis</code> 的安装包含了<code>redis-trib.rb</code>，用于创建集群</p>
</li>
<li><p>在这两台机器之一进行操作</p>
</li>
<li><p>将命令复制，可以在任意目录调用此命令 </p>
<ul>
<li><code>sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/</code></li>
</ul>
</li>
<li><p>安装ruby环境，因为<code>redis-tirb.rb</code>是ruby开发的</p>
<ul>
<li><p><code>sudo apt-get install ruby</code></p>
</li>
<li><p>提示信息输入 y ， 然后回车继续安装</p>
</li>
</ul>
</li>
</ul>
<ul>
<li><h3 id="创建集群命令："><a href="#创建集群命令：" class="headerlink" title="创建集群命令："></a>创建集群命令：</h3><ul>
<li><h3 id="redis-trib-rb-create-replicas-1-所有启动redis服务ip加端口"><a href="#redis-trib-rb-create-replicas-1-所有启动redis服务ip加端口" class="headerlink" title="redis-trib.rb create --replicas 1 所有启动redis服务ip加端口"></a><code>redis-trib.rb create --replicas 1 所有启动redis服务ip加端口</code></h3></li>
</ul>
</li>
</ul>
<h4 id="报错处理"><a href="#报错处理" class="headerlink" title="报错处理"></a>报错处理</h4><ul>
<li>原因： ruby版本过低，因为天朝防火墙，所以需要设置 gem 的源<ul>
<li>查看只记得 gem 源是什么地址<ul>
<li><code>gem source -l</code>    如果是<a href="https://rubygems.org需要就更换" target="_blank" rel="noopener">https://rubygems.org需要就更换</a></li>
</ul>
</li>
<li>更换指令为<ul>
<li>gem sources –add <a href="https://gems.ruby-china.org/" target="_blank" rel="noopener">https://gems.ruby-china.org/</a> –remove <a href="https://rubygems.org/" target="_blank" rel="noopener">https://rubygems.org/</a></li>
</ul>
</li>
<li>通过 gem 安装<code>redis</code>的相关依赖<ul>
<li><code>sudo gem install redis</code></li>
</ul>
</li>
<li>然后重新执行创建指令</li>
<li>提示完成，集群搭建成功</li>
<li>可以看到主从关系</li>
</ul>
</li>
</ul>
<h3 id="数据验证-c-连接集群"><a href="#数据验证-c-连接集群" class="headerlink" title="数据验证 -c 连接集群"></a>数据验证 -c 连接集群</h3><ul>
<li><code>redis-cli -n 主机IP -c -p 要链接的redis端口</code></li>
</ul>
<p>Ps：连接完成后，就可以正常使用了</p>
<p>它里边有一个槽的概念，存储数据会自动 匹配数据要存放的服务器。自动跳转，每个集群至少有三个主服务（节点）</p>
<h1 id="python-和-redis-集群交互"><a href="#python-和-redis-集群交互" class="headerlink" title="python 和 redis 集群交互"></a><code>python</code> 和 <code>redis</code> 集群交互</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">例：</span><br><span class="line"><span class="keyword">from</span> rediscluster <span class="keyword">import</span> StrictRedisCluster</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="comment"># 构建所有节点，redis会使用CRC16算法，将键和值写到某个节点上</span></span><br><span class="line">        startup_nodes=[</span><br><span class="line">            &#123;<span class="string">'host'</span>:<span class="string">'redis集群服务的主服务ip'</span>,<span class="string">'port'</span>:<span class="string">'对应端口'</span>&#125;,</span><br><span class="line">            &#123;<span class="string">'host'</span>:<span class="string">'redis集群服务的主服务ip'</span>,<span class="string">'port'</span>:<span class="string">'对应端口'</span>&#125;,</span><br><span class="line">            &#123;<span class="string">'host'</span>:<span class="string">'redis集群服务的主服务ip'</span>,<span class="string">'port'</span>:<span class="string">'对应端口'</span>&#125;,    <span class="comment"># 只写主从的主服务</span></span><br><span class="line">        ]</span><br><span class="line">        <span class="comment"># 构建StrictRedisCluster对象</span></span><br><span class="line">        src = StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=<span class="literal">True</span>)</span><br><span class="line">        <span class="comment"># 设置键为 name 值为 chenkai 的数据</span></span><br><span class="line">        result= src.set(<span class="string">'name'</span>,<span class="string">'chenkai'</span>)</span><br><span class="line">        print(result)</span><br><span class="line">        name = src.get(<span class="string">'name'</span>)</span><br><span class="line">    	print(name)</span><br><span class="line">	<span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(e)</span><br></pre></td></tr></table></figure>

<h2 id="总结：-2"><a href="#总结：-2" class="headerlink" title="总结："></a>总结：</h2><p>集群就是一组通过网络连接的计算机，共同对外提供服务，想一个独立的服务器！</p>

    </div>

    

    
        <div class="post-tags">
            <i class="fa fa-tags" aria-hidden="true"></i>
            <a href="/blog/tags/redis/">#redis</a> <a href="/blog/tags/redis集群/">#redis集群</a>
        </div>
    

    <!-- Comments -->
    

</div>
        </section>

    </div>
</div>


</div>

<!-- Footer -->
<div class="push"></div>

<footer class="footer-content">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 footer-about">
                <h2>About</h2>
                <p>
                    This theme was developed by <a href="https://github.com/klugjo">Jonathan Klughertz</a>. The source code is available on Github. Create Websites. Make Magic.
                </p>
            </div>
            
    <div class="col-xs-6 col-sm-6 col-md-3 col-lg-3 recent-posts">
        <h2>Recent Posts</h2>
        <ul>
            
            <li>
                <a class="footer-post" href="/blog/2019/10/25/tars框架环境基础搭建/">tars框架环境基础搭建</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/10/20/认识tars框架/">认识tars框架</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/10/05/redis数据迁移备份与恢复/">redis数据迁移备份与恢复</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/09/21/负载均衡的5种策略/">负载均衡的5种策略</a>
            </li>
            
        </ul>
    </div>



            
        </div>
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <ul class="list-inline footer-social-icons">
                    
                    <li class="list-inline-item">
                        <a href="https://github.com/klugjo/hexo-theme-alpha-dust">
                            <span class="footer-icon-container">
                                <i class="fa fa-github"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://twitter.com/?lang=en">
                            <span class="footer-icon-container">
                                <i class="fa fa-twitter"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.facebook.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-facebook"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.instagram.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-instagram"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://dribbble.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-dribbble"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://plus.google.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-google-plus"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.behance.net/">
                            <span class="footer-icon-container">
                                <i class="fa fa-behance"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://500px.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-500px"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="mailto:test@example.com">
                            <span class="footer-icon-container">
                                <i class="fa fa-envelope-o"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="\#">
                            <span class="footer-icon-container">
                                <i class="fa fa-rss"></i>
                            </span>
                        </a>
                    </li>
                    
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div class="footer-copyright">
                    @Untitled. All right reserved | Design & Hexo <a href="http://www.codeblocq.com/">Jonathan Klughertz</a>
                </div>
            </div>
        </div>
    </div>
</footer>

<!-- After footer scripts -->

<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Tween Max -->
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.18.5/TweenMax.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Custom JavaScript -->
<script src="/blog/js/main.js"></script>

<!-- Disqus Comments -->



</body>

</html>